package org.dromara.fms.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.web.core.BaseService;
import org.springframework.stereotype.Service;
import org.dromara.fms.domain.bo.FmsClassificationItemBo;
import org.dromara.fms.domain.vo.FmsClassificationItemVo;
import org.dromara.fms.domain.FmsClassificationItem;
import org.dromara.fms.mapper.FmsClassificationItemMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 财务项目Service业务层处理
 *
 * @author wenrui Su
 * @date 2024-03-22
 */
@RequiredArgsConstructor
@Service
public class FmsClassificationItemServiceImpl extends BaseService {

  private final FmsClassificationItemMapper baseMapper;

  /**
   * 查询财务项目
   */
  public FmsClassificationItemVo queryById(Long id){
    return baseMapper.selectVoById(id);
  }

  /**
   * 查询财务项目列表
   */
  public TableDataInfo<FmsClassificationItemVo> queryPageList(FmsClassificationItemBo bo, PageQuery pageQuery) {
    LambdaQueryWrapper<FmsClassificationItem> lqw = buildQueryWrapper(bo);
    Page<FmsClassificationItemVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
    return TableDataInfo.build(result);
  }

    /**
     * 查询财务项目列表
     */
    public List<FmsClassificationItemVo> queryList(FmsClassificationItemBo bo) {
        LambdaQueryWrapper<FmsClassificationItem> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<FmsClassificationItem> buildQueryWrapper(FmsClassificationItemBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<FmsClassificationItem> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getClassificationCode()), FmsClassificationItem::getClassificationCode, bo.getClassificationCode());
        lqw.like(StringUtils.isNotBlank(bo.getName()), FmsClassificationItem::getName, bo.getName());
        return lqw;
    }

    /**
     * 新增财务项目
     */
    public Boolean insertByBo(FmsClassificationItemBo bo) {
        FmsClassificationItem add = MapstructUtils.convert(bo, FmsClassificationItem.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改财务项目
     */
    public Boolean updateByBo(FmsClassificationItemBo bo) {
        FmsClassificationItem update = MapstructUtils.convert(bo, FmsClassificationItem.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(FmsClassificationItem entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除财务项目
     */
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
